package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.UsersMapper;
import com.manage.pojo.Users;
import com.manage.utils.PagedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class UsersServiceImpl implements UsersService{

    @Autowired
    private UsersMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsersIsExit(String userId) {
        Users user = new Users();
        user.setUserId(userId);
        Users result = userMapper.selectOne(user);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveUsers(Users user) {
        userMapper.insert(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String userId, String userPassword) {
        // mybatis 示例类创建
        Example userExample = new Example(Users.class);
        Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("userPassword", userPassword);
        Users result = userMapper.selectOneByExample(userExample);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void userDelete(String userId) {
        Example userExample = new Example(Users.class);
        Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("userId", userId);
        userMapper.deleteByExample(userExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUsersInfo(String userId) {
        Example userExample = new Example(Users.class);
        Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("userId", userId);
        Users result = userMapper.selectOneByExample(userExample);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getAllUsers(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Users> list = userMapper.queryAllUsers();
        PageInfo<Users> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateUserInfo(Users user) {
        Example userExample = new Example(Users.class);
        Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("userId", user.getUserId());
        userMapper.updateByExampleSelective(user, userExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateUserInfoByPassword(String userId, String userPassword) {
        userMapper.updateUserByPassword(userId, userPassword);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getAllUsersByUserIsBussiness(Integer page, Integer pageSize, Integer userIsBussiness) {
        PageHelper.startPage(page, pageSize);
        List<Users> list = userMapper.queryAllUsersByUserIsBussiness(userIsBussiness);
        PageInfo<Users> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
